package com.neuedu.oasystem.service.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import com.neuedu.oasystem.dao.IPlanDao;
import com.neuedu.oasystem.dao.impl.PlanDaoImpl;
import com.neuedu.oasystem.entity.Plan;
import com.neuedu.oasystem.service.PlanService;
import com.neuedu.oasystem.util.DBHelper;
/**
 * 计划审核表实现类
 * @author hd
 */
public class PlanServiceImpl implements PlanService{

	private IPlanDao planDao;

	public PlanServiceImpl() {
		super();
		this.planDao = new PlanDaoImpl();
	}
	
	@Override
	public boolean add(Plan param) {
				Connection connection = DBHelper.openConnection();
				try {
					connection.setAutoCommit(false);
					
					planDao.setConnection(connection);
					boolean ret = planDao.add(param);
					
					connection.commit();
					connection.setAutoCommit(true);
					return ret;
				} catch (Exception e) {
					if(connection!=null) {
						try {
							connection.rollback();
							connection.setAutoCommit(true);
						} catch (Exception e2) {
						}	
					}
				} finally {
					DBHelper.closeAll(null, null, connection);
				}
				return false;
	}

	@Override
	public boolean edit(Plan param) {
		Connection conn = DBHelper.openConnection();
		try {
			conn.setAutoCommit(false);
			planDao.setConnection(conn);
			boolean ret = planDao.edit(param);
			conn.commit();
			conn.setAutoCommit(true);
			return ret;
		} catch (SQLException e) {
			if (conn!=null) {
				try {
					conn.rollback();
					conn.setAutoCommit(true);
				} catch (Exception e2) {
				}
			} 
			e.printStackTrace();
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return false;
	}

	@Override
	public boolean deleteById(int id) {
		Connection conn = DBHelper.openConnection();
		try {
			conn.setAutoCommit(false);
			planDao.setConnection(conn);
			boolean ret = planDao.deleteById(id);
			conn.commit();
			conn.setAutoCommit(true);
			return ret;
		} catch (SQLException e) {
			if (conn!=null) {
				try {
					conn.rollback();
					conn.setAutoCommit(true);
				} catch (Exception e2) {
				}
			} 
			e.printStackTrace();
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return false;
	}

	@Override
	public Plan findById(int id) {
		Connection conn = DBHelper.openConnection();
		try {
			conn.setAutoCommit(false);
			planDao.setConnection(conn);
			Plan plan = planDao.findById(id);
			conn.commit();
			conn.setAutoCommit(true);
			return plan;
		} catch (SQLException e) {
			if (conn!=null) {
				try {
					conn.rollback();
					conn.setAutoCommit(true);
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
				
			}
			e.printStackTrace();
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return null;
	}

	@Override
	public List<Plan> findAll() {
		Connection conn = DBHelper.openConnection();
		try {
			planDao.setConnection(conn);
			List<Plan> list = planDao.findAll();
			return list;
		} catch (Exception e) {
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		
		return null;
	}

	@Override
	public List<Plan> findByExample(Plan param) {
		Connection conn = DBHelper.openConnection();
		try {
			planDao.setConnection(conn);
			List<Plan> list = planDao.findByExample(param);
			return list;
		} catch (Exception e) {
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return null;
	}

	@Override
	public List<Plan> findByPager(int page, int size) {
		Connection conn = DBHelper.openConnection();
		try {
			planDao.setConnection(conn);
			List<Plan> list = planDao.findByPager(page, size);
			return list;
		} catch (Exception e) {
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return null;
	}

	@Override
	public int deleteByIds(int[] ids) {
		Connection conn = DBHelper.openConnection();
		try {
			planDao.setConnection(conn);
			int id  = planDao.deleteByIds(ids);
			return id;
		} catch (Exception e) {
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return 0;
	}

	@Override
	public int addAll(List<Plan> useparamrs) {
		Connection conn = DBHelper.openConnection();
		try {
			planDao.setConnection(conn);
			int all  = planDao.addAll(useparamrs);
			return all;
		} catch (Exception e) {
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return 0;
	}

	@Override
	public int editAll(List<Plan> param) {
		Connection conn = DBHelper.openConnection();
		try {
			planDao.setConnection(conn);
			int all  = planDao.editAll(param);
			return all;
		} catch (Exception e) {
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return 0;
	}

}
